const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('-')}`
}
/* 
num 获取当天多少天后的日期
*/
const getDateByDay = (num) => {
  
  let today = new Date().getTime();
  let date = today + 60 * 60 * 1000 * 24 * num
  // 返回年月日格式
  return formatTime(new Date(date))
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
//成功提示  txt提示文字     time提示时间
const showSuccess = function (txt = "提示", time = 2000) {
  wx.showToast({
    title: txt,
    icon: "success",
    duration: time,
  })
}


//文字提示  txt提示文字     time提示时间
const showText = function (txt = "提示", time = 2000, fun) {
  wx.showToast({
    title: txt,
    icon: "none",
    duration: time,
    success(res) {
      typeof fun == "function" && fun(res)
    }
  })
}

//显示loading
const showLoading = function (txt,auto = true) {
  txt = txt || "加载中...";
  wx.showLoading({
    title: txt,
  })
  if(auto){
    setTimeout(function () {
      wx.hideLoading()
    }, 2000)
  }
}
// 设置顶部状态
const setTopColor = () => {
  wx.setNavigationBarColor({
    backgroundColor: '',
    frontColor: '#ffffff',
  })
}
/**
 * 获取链接的参数
 * @param {*} name 参数名
 * @param {*} url 链接
 * 如：getUrlParam('id', url)
 * @returns 
 */
const getUrlParam =  (name, url)=> {
  if (!url) url = location.href;
  var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
  var returnValue;
  for (var i = 0; i < paraString.length; i++) {
      var tempParas = paraString[i].split('=')[0];
      var parasValue = paraString[i].split('=')[1];
      if (tempParas === name)
          returnValue = parasValue;
  }
  if (!returnValue) {
      return "";
  } else {
      if (returnValue.indexOf("#") != -1) {
          returnValue = returnValue.split("#")[0];
      }
      return returnValue;
  }
}

module.exports = {
  formatTime,
  setTopColor,
  showText,
  showSuccess,
  getDateByDay,
  showLoading,
  getUrlParam
}